{% extends "base.html" %}
{% block content %}
<script type="text/javascript" src="/static/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.cookie.min.js"></script>
<script type="text/javascript" src="/static/js/artDialog/jquery.artDialog.js?skin=twitter"></script>
<script type="text/javascript" src="/static/js/artDialog/plugins/iframeTools.js"></script>
{{ttt}}
{% ifequal is_con 0 %}
<div class="form-div">
  <form action="" method="get" name="searchForm">
    <img src="/static/images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
	Program:
	<select name="p_id" id="p_id"  style='color:#CC33FF'>
		<option value="0" {% ifequal p_id 0 %} selected="selected" {% endifequal %}>All</option>
		{% for p in project_list %}
			<option value="{{p.id}}" {% ifequal p.id p_id %} selected="selected" {% endifequal %}>{{p.name}}</option>
		{% endfor %}
	</select>
	Agent:
	<select name="a_id" id="a_id" >
		<option value="0" {% ifequal a_id 0 %} selected="selected" {% endifequal %}>All</option>
		{% for a in agent_list %}
			<option value="{{a.id}}" {% ifequal a.id a_id %} selected="selected" {% endifequal %}>{{a.name}}</option>
		{% endfor %}
	</select>
	Sales:
	<input type="text" name="sales" id="sales" value="{% if sales %}{{sales}}{%endif%}"  />
	<input type="button" value="Search" id='search' class="g-button g-button-submit"/>
	<input type="button" value="Back" id='back' class="g-button g-button-submit"/>
  </form>
</div>
<script>
$(function(){
	$("#search").bind({
		click:function(){
			var t = this
			if(t.style.cursor == 'wait'){
				return false;
			}
			t.style.cursor = 'wait'
			var data = $('form[name="searchForm"]').serializeArray()
			var datas = {'is_con':1}
			for(var i=0;i<data.length;i++){
				datas[data[i].name] = data[i].value
			}
			$.ajax({
				url:'',
				data:datas,
				type:'get',
				dataType:'html',
				success:function(e){
					t.style.cursor = 'pointer'
					$('#return').html(e)
				}
			})
		}
	})

	$('#back').click(function(){
		window.location.href="/gmaillist"
	})
});
</script>
{% endifequal %}
<div id="return">
	<table width="100%" align="center" cellpadding="1" class="aui-ibox-data aui-dotted-split">
	  <thead>
		<tr>
			<th align="left" width="50"><input type='checkbox' class='selectall'>All</th>
			<th align="left" width="250">UserName</th>
			<th align="left" width="250">Email</th>
		</tr>
	  </thead>
	  <tbody>
	  {% if members %}
		{%for m in members %}
		<tr>
			<td align="left"><input type='checkbox' email="{{m.1}}" name='seluser' class="sel-val"></td>
			<td align="left">{{m.0}}</td>
			<td align="left">{{m.1}}</td>
		</tr>
		{% endfor %}
		<tr>
			<td align="left"><input type='checkbox' class='selectall'>All</td>
			<td colspan='5' style="color:green;">MailListName：<input type="text" name='listname'><input type="button" name="createList" class="g-button g-button-submit" value="Create">&nbsp;Choose Total:&nbsp;<span style="color:red;" id="selectnum">0</span>&nbsp;</td>
		</tr>
	  {%else%}
	    <tr>
			<td align="center" valign="center" height="60" colspan="10" style="color:green;font-size:16px;line-height: 60px;">No Result !</td>
		</tr>
	  {%endif%}
		<tr>
			<td align="center" colspan="10">
				<div id="pages" class="pager"></div>
			</td>
		</tr>
	  </tbody>
	</table>
	<input type='hidden' name='maillist' id='maillist' value=''/>
	<input type='hidden' name='listid' id='listid' value=''/>
<script>
$(function(){
	$('input[name="createList"]').click(function(){
		var t = this
		if(t.style.cursor == 'wait')return false;
		$(t).val('Create...')
		t.style.cursor='wait'
		var maillist = $('input[name="maillist"]').val()
		var listname = art.dialog.data('listname');
		var host = location.host
		var listid = $('input[name="listid"]').val()
		if(maillist == ''){
			art.dialog.tips('Please select at least one user！',3)
			$(t).val('Create')
			t.style.cursor = 'pointer'
			return false;
		}else{
			var dialog = art.dialog({id: 'N3690',title:'Create Mail List ...',lock:true});
			$.ajax({
				url:'/gcreatlist/',
				type:'post',
				data:{'maillist':maillist,'listname':listname,'listid':listid,'host':host},
				success:(function(e,code){
					if(code == 'success'){
						var e = JSON.parse(e);
						if(e['code']==1){
							dialog.lock()
							dialog.content('New mailing list sucessfully added.')
							dialog.button({name:'OK',callback:(function(){window.location.href='/gmaillist'})})
							$('input[name="listid"]').val('')
						}
						dialog.content(e['info'])
						dialog.button({name:'OK'})
						dialog.lock()
						if(e['ids']!=undefined && e['ids']!=''){
							$('input[name="listid"]').val(e['ids'])
						}
					}else{
						dialog.title('ERROE')
						dialog.content('Fail!')
					}
					$(t).val('Create')
					t.style.cursor = 'pointer'
				})
			})
		}
		
	})
	$(".selectall").bind("click", function () {
        var $checked = $(this).attr("checked");
        $("input[type='checkbox'], .selectall").attr("checked", $checked);
		if(this.checked){
			$("input[type='checkbox'].sel-val").each(function(){
				addemail(this)
			})
		}else{
			$("input[type='checkbox'].sel-val").each(function(){
				delemail(this)
			})
		}
    });
	$("input[type='checkbox'].sel-val").click(function(){
		if(this.checked){
			addemail(this)
		}else{
			delemail(this)
		}
	})

	$('input[name="listname"]').blur(function(){
		var listname = $(this).val()
		art.dialog.data('listname',listname);	
	})
	function gourl(obj){
		var url = $(obj).attr('href')
		$.get(url,function(e){$('#return').html()})
	}

	function initdata(){
		var nums = art.dialog.data('nums');
		var emails = art.dialog.data('emails');
		var listname = art.dialog.data('listname');
		if(emails != undefined && nums != undefined){
			$("input[type='checkbox'].sel-val").each(function(){
				if(emails.indexOf($(this).attr('email')) != -1){
					this.checked = true
				}
			})
			$('#maillist').val(emails)
			$('span#selectnum').text(nums)
			$('input[name="listname"]').val(listname)
		}
		var tolpage = {{tolpage}}
		if(tolpage<=1)return false;
		for(var i=1;i<=tolpage;i++){
			var c = ''
			if(i=={{page}}){
				c = 'active'
			}
			$('#pages').append("<li><a href='?is_con=1&p_id={{p_id}}&a_id={{a_id}}&sales={% if sales %}{{sales}}{%endif%}&page="+i+"' onclick=\"$.get(this.href,function(e){$(\'#return\').html(e)});return false;\" class='"+c+"'>"+i+"</a></li>")
		}
	}

	function settopnum(){
		var tol = parseInt($('span#selectnum').text())
		var emails = $('#maillist').val()
		var listname = art.dialog.data('listname');
		art.dialog.data('nums', tol);
		art.dialog.data('emails', emails);
	}
	function addemail(obj){
		var newemail = $(obj).attr('email')
		var oldemail = $('#maillist').val()
		if(oldemail.indexOf(newemail) == -1){
			$('#maillist').val(oldemail+','+newemail)
			$('span#selectnum').text(parseInt($('span#selectnum').text())+1)
		}
		settopnum()
	}
	function delemail(obj){
		var newemail = $(obj).attr('email')
		var oldemail = $('#maillist').val()
		if(oldemail.indexOf(newemail) != -1){
			$('#maillist').val(oldemail.replace(','+newemail,''))
			$('span#selectnum').text(parseInt($('span#selectnum').text())-1)
		}
		settopnum()
	}
	initdata(); //初始化页面数据
});
</script>
</div>
{% endblock %}
